//
//  MYGMedicalToolsWalkController.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/4/21.
//

import UIKit

class MYGMedicalToolsWalkController: MYGBaseController {
    // MARK: - Property
    private var height: Float?
    private var weight: Float?
    private var sex: Int?
    private var age: Int?
    private var distance: Float?

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        let separator1 = createSeparator()
        let separator2 = createSeparator()
        let separator3 = createSeparator()
        let separator4 = createSeparator()
        let separator5 = createSeparator()
        view.addSubviews([
            ageInputView,
            separator1,
            sexChooseView,
            separator2,
            heightInputView,
            separator3,
            weightInputView,
            separator4,
            distanceInputView,
            separator5,
            scoreView,
            commitButton
        ])
        
        ageInputView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
                
        separator1.snp.makeConstraints { make in
            make.top.equalTo(ageInputView.snp.bottom).offset(8)
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
        }
        
        sexChooseView.snp.makeConstraints { make in
            make.top.equalTo(separator1.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator2.snp.makeConstraints { make in
            make.top.equalTo(sexChooseView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        heightInputView.snp.makeConstraints { make in
            make.top.equalTo(separator2.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator3.snp.makeConstraints { make in
            make.top.equalTo(heightInputView.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        weightInputView.snp.makeConstraints { make in
            make.top.equalTo(separator3.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator4.snp.makeConstraints { make in
            make.top.equalTo(weightInputView.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        distanceInputView.snp.makeConstraints { make in
            make.top.equalTo(separator4.snp.bottom).offset(0)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        separator5.snp.makeConstraints { make in
            make.top.equalTo(distanceInputView.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(separator5.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    // MARK: - lazy load
    private lazy var heightInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .height, frame: .zero)
        view.inputDidChange = {[weak self] (height) in
            self?.height = Float(height)
        }
        return view
    }()
    
    private lazy var weightInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .weight, frame: .zero)
        view.inputDidChange = {[weak self] (weight) in
            self?.weight = Float(weight)
        }
        return view
    }()
    
    private lazy var ageInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .age, frame: .zero)
        view.inputDidChange = {[weak self] (age) in
            self?.age = Int(age)
        }
        return view
    }()
    
    private lazy var distanceInputView: MYGMedicalToolsInputView = {
        let view = MYGMedicalToolsInputView(type: .custom(title: "距离", unit: "m", placeholder: "请输入距离", keyboardType: .decimalPad), frame: .zero)
        view.inputDidChange = {[weak self] (distance) in
            self?.distance = Float(distance)
        }
        return view
    }()
    
    
    private lazy var sexChooseView: MYGMedicalToolsChooseView = {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = "性别"
        view.leftTitle = "男"
        view.rightTitle = "女"
        view.selectedAction = {[weak self] (sex) in
            self?.sex = sex
        }
        return view
    }()
    
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "不达标"
        view.title = "0%"
        return view
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
    
}

extension MYGMedicalToolsWalkController {
    @objc func commit() {
        view.endEditing(true)
        guard let height = height, let weight = weight, let sex = sex, let age = age, let distance = distance else { return }
        if height < 0 || height > 300 { return }
        if weight < 0 || weight > 500 { return }
        var value: Float = 0.0
        if age == 0 {
            value = 7.57 * height - 5.02 * Float(age) - 1.76 * weight - 309
        } else {
            value = 2.11 * height - 5.78 * Float(age) - 2.29 * weight + 667
        }
        
        let progress = distance / value
        if progress < 1 {
            scoreView.level = "不达标"
        } else {
            scoreView.level = "正常"
        }
        scoreView.progress = Double(progress)
        scoreView.title = String(format: "%0.f%%", progress*100)
    }
}

